$(document).ready(function () {


	//-----------------------------------------定义和初始化变量----------------------------------------
	var loadBox = $('aside.loadBox');
	var articleBox = $('article');
	var windowScale = window.innerWidth / 750;


	resetShare();

	//----------------------------------------有微信授权放这里  授权完在 icom.init(init)----------------------------------------





	icom.init(init);//初始化
	icom.screenScrollUnable();//如果是一屏高度项目且在ios下，阻止屏幕默认滑动行为

	function init() {
		requestAnimationFrame(function () {
			var screenProp = window.innerWidth / window.innerHeight
			console.log("os.screenProp:" + os.screenProp);
			if (screenProp < 0.54) articleBox.addClass("screen189");
			if (screenProp > 0.64) articleBox.addClass("screen159");
			sound_handler();
			load_handler();
		});
	}//edn func


	//----------------------------------------加载页面图片----------------------------------------
	function load_handler() {
		var loader = new PxLoader();
		loader.addImage('images/common/turn_phone.png');
		loader.addImage('images/index/logo.png');

		loader.addImage('images/p1/box.png');
		loader.addImage('images/p1/btn.png');
		loader.addImage('images/p1/i1.png');
		loader.addImage('images/p1/mask.png');
		loader.addImage('images/p1/people.png');
		loader.addImage('images/p1/tip.png');
		loader.addImage('images/p1/tree.png');

		loader.addImage('images/p2/box.png');
		loader.addImage('images/p2/gifts.png');
		loader.addImage('images/p2/i2.png');

		loader.addImage('images/p3/box.png');
		loader.addImage('images/p3/caidai.png');
		loader.addImage('images/p3/rule_icon.png');
		loader.addImage('images/p3/star.png');
		loader.addImage('images/p3/start.png');
		loader.addImage('images/p3/text.png');

		loader.addImage('images/p4/ball.png');
		loader.addImage('images/p4/box.png');
		loader.addImage('images/p4/box2.png');
		loader.addImage('images/p4/gold_icon.png');

		for (var i = 1; i < 11; i++) {
			loader.addImage('images/p4/i' + i + '.png');
		}

		loader.addImage('images/p4/light_bg.png');
		loader.addImage('images/p4/star_group.png');
		loader.addImage('images/p4/text.png');

		loader.addImage('images/p5/box.png');
		loader.addImage('images/p5/caidai1.png');
		loader.addImage('images/p5/caidai2.png');
		loader.addImage('images/p5/gift.png');
		loader.addImage('images/p5/more.png');
		loader.addImage('images/p5/tree.png');

		loader.addImage('images/p6/again.png');
		loader.addImage('images/p6/box.png');
		loader.addImage('images/p6/caidai2.png');
		loader.addImage('images/p6/share.png');
		loader.addImage('images/p6/text.png');

		//实际加载进度
		//		loader.addProgressListener(function(e) {
		//			var per=Math.round(e.completedCount/e.totalCount*50);
		//			loadPer.html(per+'%');
		//		});

		loader.addCompletionListener(function () {
			loadBox.hide();
			icom.fadeIn(articleBox);
			pageInit();
			//			load_timer(50);//模拟加载进度
			loader = null;
		});
		loader.start();
	}//end func


	function sound_handler() {
		if (os.weixin) wx.ready(sound_creat);
		else sound_creat();
	}//edn func

	var soundList = "";

	function sound_creat() {
		console.log('sound_creat');
		//bgm
		//webaudio在ios下自动播放目前不行
		ibgm.init({ src: 'sound/bgm.mp3', autoplay: os.weixin || os.taobao, webAudio: false, onLoaded: bgm_loaded_complete });
		//audio
		soundList = iaudio.on([{ src: 'sound/btn.mp3', onTimeupdate: audio_timeupdate_handler }], { onProgress: soundList_loaded_progress, onLoadComplete: soundList_loaded_complete });
		//webaudio
		//		soundList=iaudio.on([{src:'sound/sound.mp3',onTimeupdate:webaudio_timeupdate_handler,volume:1}],{onProgress:soundList_loaded_progress,onLoadComplete:soundList_loaded_complete,webAudio:1});

		//测试mp3转base64
		var sound = new Audio;
		// sound.src=base64Sound;
		sound.play();
	}//end func

	function audio_timeupdate_handler(event) {
		//		console.log(this.currentTime/this.duration*100+'%');
	}//edn func

	function webaudio_timeupdate_handler(_this) {
		//		console.log(_this.context.currentTime/_this.buffer.duration*100+'%');
	}//edn func

	function bgm_loaded_complete() {
		console.log('bgm load complete');
		console.log(ibgm.audio);

		setTimeout(function () {
			//			ibgm.pause();
			//			ibgm.hide(true);
			//			console.log(ibgm.currentTime());
		}, 1000);

		setTimeout(function () {
			//			ibgm.play();
		}, 2000);

	}//edn func

	function soundList_loaded_progress(progress) {
		console.log('progress:' + progress);
	}//edn func

	function soundList_loaded_complete() {
		console.log(soundList.sound);
		// init_handler();
	}//edn func

	// 熄屏
	// document.addEventListener("visibilitychange", function(){
	// 	if (document.hidden) {
	// 		ibgm.pause();
	// 	} else {
	// 	  setTimeout(function(){
	// 		ibgm.play();
	// 	  }, 500)
	// 	}
	// });

	//模拟加载进度
	function load_timer(per) {
		per = per || 0;
		per += imath.randomRange(1, 3);
		per = per > 100 ? 100 : per;
		loadPer.html(per + '%');
		if (per == 100) setTimeout(pageInit, 200);
		else setTimeout(load_timer, 33, per);
	}//edn func

	//----------------------------------------页面逻辑代码----------------------------------------

	



	var swiper = "";
	var is_flag = false;//按下变成true
	var cloverList = [];//四叶草
	var shimaoList = ["璀璨兰庭", "观溪和园", "云著深蓝", "世茂云溪阁", "世茂九溪墅", "世茂璀璨星河", "天台樾湖兰庭", "世茂世纪中心", "世茂璀璨天宸", "世茂云图花园"];
	var realIndex = 0;
	var click_flag = true;

	/**
	 * 页面初始化
	 */
	function pageInit() {
		// silenceAuth();
		monitor_handler();
		// icom.countdown($('.btnCode'), 60, '#s');

		// $(".p1 .start").on("click", p1Start);

		p1Start();

		$(".p2 .start").on("click", p2Start);
		$(".p3 .start").on("click", p3Start);
		$(".p5 .more_icon").on("click", p5Start);


		// 再来一次
		$(".p6 .menu_again").on("click", menu_again);

		// 分享好友
		$(".p6 .menu_share").on("click", menu_share);
		// 隐藏分享好友弹窗
		$(".share_mask").on("click", share_mask);

		// 按
		$(".p4 .start").on("click", p4Start);

	}//end func


	// 静默授权
	function silenceAuth(){
		var appid = "wxe65a7a45459d3cf0";
		var url = window.location.href;
		window.location.href = 'http://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + url + '&response_type=code&scope=snsapi_base&state=123#wechat_redirect'
	}

	// 分享
	function resetShare() {
		ishare.wxShareSuccess = function () {//分享成功后回调函数
			imonitor.add({ category: 'default', label: '分享成功' });
		}//end func;
		ishare.wxShareCancel = function () {//分享取消后回调函数
			imonitor.add({ category: 'default', label: '分享失败' });
		}//end func;

		var share = {};
		share.title = $('title').html();	 //好友标题
		share.friend = '点击滚动>>>';	//好友内容
		share.timeline = share.other = $('title').html();	//朋友圈
		ishare.reset(share);
	}



	// 页面跳转
	function p1Start() {
		setTimeout(function () {
			$(".p1").hide();
			$(".p2").show();
			$(".people").hide();
			$(".gifts").show();
		}, 6500)
	}


	function p2Start() {
		$(".p2").hide();
		$(".p3").show();

		$(".people").show();
		$(".gifts").hide();
	}


	function p3Start() {
		$(".p3").hide();
		$(".p4").show();
		p4_init_swiper();
	}

	var ballTop = "", ballLeft = "";
	// 按 开始游戏
	function p4Start() {
		if (click_flag) {
			click_flag = false;
			$(this).addClass("bounceIn");
			$(".ball").addClass("ball_an fadeIn");
			is_flag = true;
			// console.log('cloverList start: ', cloverList); 

			// setTimeout(function(){
			// 	ballTop = $(".p4 .ball").offset().top;
			// 	console.log('ballTop: ', ballTop);
			// 	ballLeft = $(".p4 .ball").offset().left;
			// 	console.log('ballLeft: ', ballLeft);
			// },1800)


			setTimeout(function () {
				$(".ball").removeClass("ball_an fadeIn");
				$(".p4 .start").removeClass("bounceIn");
				is_flag = false;
				click_flag = true;
				
				if (gifts_status) {
					cloverList.push(shimaoList[realIndex]);
					$(".p4 .text_num").text(cloverList.length);
					soundList.btn.play();
				}

				if (cloverList.length >= 5) {
					setTimeout(function () {
						// 跳转页面 并且初始化数据
						$(".p4").hide();
						$(".p5").show();
						swiper.destroy();
						$(".people").hide();
						cloverList = [];
						realIndex = 0;
						is_flag = false;
						click_flag = true;
						$(".p4 .text_num").text(0);
					}, 300)
				}

			}, 2000)
		}

	}

	// 判断是否相互碰撞
	function is_crash() {
		// 获取礼物盒距离屏幕的高度
		var giftsDomTop = $(".shimao").offset().top - $(window).scrollTop() + $(".shimao").height();
		console.log('giftsDomTop: ', giftsDomTop);
		var giftsDomLeft = $(".shimao").offset().left + $(".shimao").width() / 2;
		console.log('giftsDomLeft: ', giftsDomLeft);


		var ballDomTop = $(".ball").offset().top;
		console.log('ballDomTop: ', ballDomTop);
		var ballDomLeft = $(".ball").offset().left;
		console.log('ballDomLeft: ', ballDomLeft);


		setTimeout(function () {
			var ballDomTop = $(".ball").offset().top;
			console.log('ballDomTop: ', ballDomTop);
			var ballDomLeft = $(".ball").offset().left;
			console.log('ballDomLeft: ', ballDomLeft);
		}, 3000)
	}


	// 删除数组最后一项
	function truncate(arr) {
		var newArr = arr.concat();
		newArr.pop();
		return newArr
	}

	var top = "", left = "", gifts_status = false;
	// 初始化 swiper
	function p4_init_swiper() {
		swiper = new Swiper('.p4 .swiper-container', {
			loop: true,
			observer:true,
			observeParents:true,
			autoplay: {
				delay: 1,
				disableOnInteraction: false,
			},
			freeMode: true,
			speed: 2000,
			slidesPerView: 1,
			slidesPerGroup: 1,
			spaceBetween:10,
			on: {
				slideNextTransitionEnd: function (swiper) {
					realIndex = this.realIndex;
					// console.log('realIndex: ', realIndex);
					// if (is_flag) {
					// 	// 如果滑动的时候 是 状态是true 说明 没有中 因为 滑动了 球还在滚动
					// 	cloverList = truncate(cloverList);
					// 	console.log('cloverList end: ', cloverList);
					// }
					// top = $(".swiper-slide-active").offset().top;
					// left = $(".swiper-slide-active").offset().left;
					// console.log('top: ', top);
					// console.log('left: ', left);
					gifts_status = true;
					setTimeout(() => {
						gifts_status = false;
					}, 1000)
				},
			}
		});
	}


	function p5Start() {
		$(".p5").hide();
		$(".p6").show();
	}


	// 再来一次
	function menu_again() {
		$(".p6").hide();
		$(".p4").show();
		$(".people").show();
		$(".share_mask").hide();
		$(".tree").hide();
		// swiper.update();
		p4_init_swiper();
	}


	// 分享好友
	function menu_share() {
		$(".share_mask").show();
	}

	function share_mask() {
		$(this).hide();
	}



	//----------------------------------------页面监测代码----------------------------------------
	function monitor_handler() {
		//		imonitor.add({obj:$('a.btnTest'),action:'touchstart',category:'default',label:'测试按钮'});
	}//end func
});//end ready
